import time

from selenium import webdriver
from selenium.webdriver.support.select import Select


class add:
    def __init__(self):
        we = webdriver.Firefox()
        s = "http://192.168.0.118"
        we.get(s+"/TinyShop//index.php?con=admin&act=login")
        self.we = we
    def login(self,user,pwd):

        we = self.we
        we.find_element_by_name("name").clear()
        we.find_element_by_name("name").click()
        we.find_element_by_name("name").send_keys(user)
        we.find_element_by_name("password").clear()
        we.find_element_by_name("password").click()
        we.find_element_by_name("password").send_keys(pwd)
        we.find_element_by_name("verifyCode").send_keys('aaaa')
        we.find_element_by_xpath("//input[@value=\"登 录\"]").click()

    def add(self,li):
        we = self.we
        we.find_element_by_link_text("商品中心").click()
        we.find_element_by_link_text("添加").click()

        Select(we.find_element_by_id('goods_type')).select_by_value(li[0]) #商品类型 1
        Select(we.find_element_by_id('category_id')).select_by_value(li[1])#分类  6
        Select(we.find_element_by_id('type_id')).select_by_value(li[2])#类型 4
        Select(we.find_element_by_name('brand_id')).select_by_value(li[3])#品牌 1
        we.find_element_by_name("name").clear()
        we.find_element_by_name("name").send_keys(li[4])  #商品名称
        we.find_element_by_name("subtitle").send_keys(li[5])#商品副标题
        we.find_element_by_name("tag_ids").send_keys(li[6])#商品关键字
        we.find_elements_by_name("goods_no").clear()
        we.find_element_by_id("goods_no").send_keys(li[7])#商品编号
        we.find_element_by_xpath("/html/body/div[3]/div[2]/form/div/div[1]/div[1]/dl[10]/dd/button").click()
        time.sleep(2)
        we.switch_to_frame(we.find_element_by_tag_name("iframe"))

        we.find_element_by_xpath("//li[2]").click()
        we.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/span").click()
        we.find_element_by_xpath("/html/body/div[2]/div/div[2]/ul/li[2]/img").click()
        we.find_element_by_xpath("/html/body/div[3]/button").click()
        self.we.switch_to.default_content()
        we.find_elements_by_name("pro_no").clear()
        we.find_element_by_name("pro_no").send_keys(li[8])#商品规格货号 与商品编号一致
        we.find_elements_by_name("store_nums").clear()
        we.find_element_by_name("store_nums").send_keys(li[9])#商品规格库存
        we.find_elements_by_name("warning_line").clear()
        we.find_element_by_name("warning_line").send_keys(li[10])#商品规格预警线
        we.find_element_by_name("weight").clear()
        we.find_element_by_name("weight").send_keys(li[11])#商品规格重量
        we.find_element_by_name("sell_price").clear()
        we.find_element_by_name("sell_price").send_keys(li[12])#商品规格零售价
        we.find_elements_by_name("warning_line").clear()
        we.find_element_by_name("market_price").send_keys(li[13])#商品规格市场价
        we.find_elements_by_name("cost_price").clear()
        we.find_element_by_name("cost_price").send_keys(li[14])#商品规格成本价
        we.find_element_by_class_name("focus_button").click()#点击添加
        return  we
    def __del__(self):
        we=self.we
        we.quit()



